/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.threadtask.workthread;

import com.xlx.openLink.base.BaseUtils;
import com.xlx.openLink.entity.pojo.Alarm;
import com.xlx.openLink.entity.pojo.Device;
import com.xlx.openLink.entity.pojo.Port;
import com.xlx.openLink.enums.AlLevel;
import com.xlx.openLink.enums.DevTypeEnum;
import com.xlx.openLink.service.AlarmService;
import com.xlx.openLink.service.DeviceService;
import com.xlx.openLink.service.PortService;
import com.xlx.openLink.vo.DevicePortVO;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
public class SearchPortThread implements Runnable {

    private List<DevicePortVO> devicePortVOList;

    private DeviceService deviceService;


    private AlarmService alarmService;

    private PortService portService;


    public SearchPortThread(List<DevicePortVO> devicePortVOList, DeviceService deviceService, PortService portService, AlarmService alarmService) {
        this.devicePortVOList = devicePortVOList;
        this.deviceService = deviceService;
        this.portService = portService;
        this.alarmService = alarmService;

    }

    @Override
    public void run() {
        devicePortVOList.forEach(devicePortVO -> {
            //判断是否是有端口
            List<Port> inList = new ArrayList<>();
            List<Port> upList = new ArrayList<>();
            List<Alarm> alarmList = new ArrayList<>();
            try {
                List<Port> ports = new ArrayList<>();
                deviceService.initIfTable(DevicePortVO2Dev(devicePortVO), ports);
                List<Port> rePort = devicePortVO.getPortList();
                for (Port p : ports) {
                    boolean flag = false;
                    for (Port rp : rePort) {
                        if (p.equals(rp)) {
                            if (!p.getpStatus().equals("1") && rp.getpStatus().equals("1")) { //掉线了
                                alarmList.add(createAlarm(p, "设备名称:" + rp.getpDevName() + "\n" + (rp.getpName() != null ? "端口名称:" + rp.getpName() : "") + "\n"
                                        + "端口编号:" + rp.getpDescr() + "\n告警信息:" + "掉线"));
                            } else if (p.getpStatus().equals("1") && !rp.getpStatus().equals("1")) {
                                alarmList.add(createAlarm(p, "设备名称:" + rp.getpDevName() + "\n" + (rp.getpName() != null ? "端口名称:" + rp.getpName() : "") + "\n"
                                        + "端口编号:" + rp.getpDescr() + "\n告警信息:" + "恢复"));
                            }
                            deviceService.initData(rp, p);
                            rp.setpDevName(devicePortVO.getDevName());
                            upList.add(rp);
                            flag = true;
                            break;
                        }
                    }
                    if (!flag) {
                        p.setpDevName(devicePortVO.getDevName());
                        inList.add(p);
                    }
                }
                if (!inList.isEmpty()) {
                    portService.insertBatch(inList);
                }
                if (!upList.isEmpty()) {
                    portService.updateBatch(upList);
                }
                if (!alarmList.isEmpty()) {
                    alarmService.insertBatch(alarmList);
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.error(e.getMessage());
            }
        });

    }

    private Device DevicePortVO2Dev(DevicePortVO devicePortVO) {
        Device device = new Device();
        device.setDevReadcommunity(devicePortVO.getDevReadcommunity());
        device.setDevIp(devicePortVO.getDevIp());
        device.setDevSnmpPort(devicePortVO.getDevSnmpPort());
        device.setPkGuid(devicePortVO.getPkGuid());
        device.setDevName(devicePortVO.getDevName());
        device.setDevSnmpVersion(devicePortVO.getDevSnmpVersion());
        return device;
    }

    private Alarm createAlarm(Port port, String msg) {
        Alarm alarm = new Alarm();
        alarm.setPkGuid(BaseUtils.createGUID());
        alarm.setAlPortId(port.getPkId());
        alarm.setAlDevId(port.getpDevId());
        alarm.setAlCreateDate(new Date());
        alarm.setAlDevIp(port.getpDevIp());
        alarm.setAlDevName(port.getpDevName());
        alarm.setAlMsg(msg);
        //交换机
        alarm.setAlType(DevTypeEnum.SwitchType.getCode());
        alarm.setAlLevel(AlLevel.AL_SEVERITY.getCode());
        alarm.setAlPortName(port.getpName());
        return alarm;
    }
}
